<?php 
/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.1
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
function resize_dis($cur_w,$cur_h,$max_w = 450,$max_h = 450)
{
	if($cur_w > $max_w || $cur_h > $max_h)
	{
		if($cur_w > $cur_h)
		{
			$new_w = $max_w;
			$new_h = ($new_w*$cur_h)/$cur_w;
			return array($new_w,$new_h);
		}
		else if($cur_h > $cur_w)
		{
			$new_h = $max_h;
			$new_w = ($new_h*$cur_w)/$cur_h;
			return array($new_w,$new_h);
		}
		else if($cur_h==$cur_w)
		{
			return array($max_w,$max_h);
		}
	}
	else
	{
		return array($cur_w,$cur_h);
	}
}

function image_resize_html($img_path,$max_width = 450,$max_height= 450)
{
	
	$tmp = explode('/',$img_path);
	$filename = $tmp[count($tmp)-1];
	
	$cache_file = 'cache/img_info_'.$filename.'.__';
	
	if(file_exists($cache_file))
	{
		$fp = fopen($cache_file,'r');
		
		$stats = fstat($fp);
		$limit = time()-(60*60*24); // One day to refresh the cache
		
		if($stats['mtime']<$limit)
		{
			fclose($fp);
			$dis = getimagesize($img_path);
			fopen($cache_file,'w');
			fwrite($fp,serialize($dis));
			
		}
		else
		{
			$file = fgets($fp);
			$dis = unserialize($file);
		}
		
		fclose($fp);
	}
	else
	{
		$dis = getimagesize($img_path);
		$fp = fopen($cache_file,'w');
		fwrite($fp,serialize($dis));
		fclose($fp);
	}
	
	
	$new_dis = resize_dis($dis[0],$dis[1],$max_width,$max_height);
	
	$html = '<img src="'.$img_path.'" width="'.$new_dis[0].'" height="'.$new_dis[1].'">';
	
	return array($dis,$new_dis,$html);
}


function defaultAvatarHTML($settings)
{
	$max_x = $settings['avatar_max_x'];
	$max_y = $settings['avatar_max_y'];
	return '<img src="images/no_avatar.jpg" width="'.$max_x.'" height="'.$max_y.'" />';
}


?>